package com.linln.wxapi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.friends.domain.Message;
import com.linln.modules.friends.domain.Relation;
import com.linln.modules.friends.service.MessageService;
import com.linln.modules.friends.service.RelationService;
import com.linln.modules.member.domain.MemberInfo;
import com.linln.modules.member.service.MemberInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value="好友列表")
@RestController
@RequestMapping("/wx/member/{area}")
public class WxApiMemberController {

	   @Autowired
	   private MemberInfoService memberInfoService;
	   @Autowired
	   private MessageService messageService;
	   @Autowired
	   private RelationService relationService;
	   
	   
	   private final Logger logger = LoggerFactory.getLogger(this.getClass());
	  /**
     * 获取好友列表
     * SEX不传为所有好友  1为男  0为女 ，2为关注用户
     * @return
     */
    @ApiOperation(value = "获取好友列表",  notes = "获取好友列表")
    @RequestMapping(value = "memberList",method={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResultVo friendsList(@PathVariable String area,String sex) {
        try {
            ExampleMatcher matcher = ExampleMatcher.matching();
            MemberInfo baseinfo=new MemberInfo();
            if(null!=sex&&!"".equals(sex)) {
            	baseinfo.setSex(sex);
            }
            Example<MemberInfo> example = Example.of(baseinfo, matcher);
            Page<MemberInfo> list = memberInfoService.getPageList(example);
            if(list.getContent()==null || list.getContent().size()==0) {
                return ResultVoUtil.error("暂无数据"); 
             }else {
             return ResultVoUtil.success(list.getContent());
            }
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            return ResultVoUtil.error("接口异常"); 
        }
 
    }
	
	/*
	 * 获取消息列表
	 */
 @ApiOperation(value = "获取消息列表",  notes = "获取消息列表")
 @RequestMapping(value = "messageList",method={RequestMethod.POST,RequestMethod.GET})
 @ResponseBody
 public ResultVo messageList(@PathVariable String area,long senderid,int page) {
     try {
         ExampleMatcher matcher = ExampleMatcher.matching();
         Message message=new Message();
         message.setSenduser(memberInfoService.getById(senderid));
         Example<Message> example = Example.of(message, matcher);
         Sort sort = new Sort(Sort.Direction.ASC, "readstate");
         sort.by(Sort.Direction.DESC, "createDate");
         
         Page<Message> list=messageService.getPageListByUserid(senderid);
         
         //Page<Message> list = messageService.getPageList(example,sort,page);
         
         if(list.getContent()==null || list.getContent().size()==0) {
             return ResultVoUtil.error("暂无数据"); 
          }else {
          return ResultVoUtil.success(list.getContent());
         }
     } catch (Exception e) {
         this.logger.error(e.getMessage(), e);
         return ResultVoUtil.error("接口异常"); 
     }

 }
 
 /*
	 * 获取消息详情
	 */
@ApiOperation(value = "获取消息详情",  notes = "获取消息详情")
@RequestMapping(value = "messageDetail",method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public ResultVo messageDetail(@PathVariable String area,long senderid,long receiverid,int page) {
  try {
      ExampleMatcher matcher = ExampleMatcher.matching();
      Message message=new Message();
      message.setSenduser(memberInfoService.getById(senderid));
      message.setReceiveuser(memberInfoService.getById(receiverid));
      Example<Message> example = Example.of(message, matcher);
      Sort sort = new Sort(Sort.Direction.DESC, "createDate");
      Page<Message> list = messageService.getPageList(example,sort,page);
      if(list.getContent()==null || list.getContent().size()==0) {
          return ResultVoUtil.error("暂无数据"); 
       }else {
       return ResultVoUtil.success(list.getContent());
      }
  } catch (Exception e) {
      this.logger.error(e.getMessage(), e);
      return ResultVoUtil.error("接口异常"); 
  }

}
    
//发送消息
@RequestMapping(value ="addMessage",method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public ResultVo addMessage(Message message) {
    try {
    	messageService.save(message);
        return ResultVoUtil.success("发送消息成功");
    } catch (Exception e) {
        this.logger.error(e.getMessage(), e);
        return ResultVoUtil.error("接口异常"); 
    }
}
     

//修改用户信息
@RequestMapping(value ="updMemberInfo",method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public ResultVo updMember(MemberInfo memberinfo) {
  try {
	  MemberInfo nmemberinfo=  memberInfoService.getById(memberinfo.getId());
	  nmemberinfo.setHeadimg(memberinfo.getHeadimg());
	  nmemberinfo.setNickname(memberinfo.getNickname());
	  nmemberinfo.setSex(memberinfo.getSex());
	  nmemberinfo.setBirthday(memberinfo.getBirthday());
	  nmemberinfo.setUserarea(memberinfo.getUserarea());
	  nmemberinfo.setOccupation(memberinfo.getOccupation());
	  memberInfoService.save(nmemberinfo);
      return ResultVoUtil.success("修改用户信息成功");
  } catch (Exception e) {
      this.logger.error(e.getMessage(), e);
      return ResultVoUtil.error("接口异常"); 
  }
}

//二次修改用户信息
@RequestMapping(value ="updMemberInfose",method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public ResultVo updMemberInfose(MemberInfo memberinfo) {
try {
	  MemberInfo nmemberinfo=  memberInfoService.getById(memberinfo.getId());
	  nmemberinfo.setHobby(memberinfo.getHobby());
	  nmemberinfo.setDescrition(memberinfo.getDescrition());
	  nmemberinfo.setEducation(memberinfo.getEducation()); 
	  nmemberinfo.setSelfdesc(memberinfo.getSelfdesc());
	  memberInfoService.save(nmemberinfo);
    return ResultVoUtil.success("修改用户信息成功");
} catch (Exception e) {
    this.logger.error(e.getMessage(), e);
    return ResultVoUtil.error("接口异常"); 
}
}


//添加关注
@RequestMapping(value ="addRelation",method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public ResultVo addRelation(Relation relation) {
try {
	  relationService.save(relation);
  return ResultVoUtil.success("添加关注成功");
} catch (Exception e) {
  this.logger.error(e.getMessage(), e);
  return ResultVoUtil.error("接口异常"); 
}
}

//取消关注
@RequestMapping(value ="delRelation",method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public ResultVo delRelation(long  userid,long ruserid) {
try {
	
	ExampleMatcher matcher = ExampleMatcher.matching();
    Relation relation=new Relation();
    relation.setUserid(userid);
    relation.setRuserid(ruserid);
    Example<Relation> example = Example.of(relation, matcher);
    Page<Relation> rpage=relationService.getPageList(example);
	if(rpage.getContent()!=null&&rpage.getContent().size()>0) {
		Relation rl=rpage.getContent().get(0);
		relationService.delid(rl.getId());
	} 
    
  return ResultVoUtil.success("取消关注成功");
} catch (Exception e) {
  this.logger.error(e.getMessage(), e);
  return ResultVoUtil.error("接口异常"); 
}
}
}


